import Vue from 'vue'
import Vuex from 'vuex'
import { Stomp } from '../websocket/stomp.js'
import UniWebSocket from '../websocket/uniWebSocket.js'
Vue.use(Vuex)
const store = new Vuex.Store({
    state: {
		val:"9966",
		client:null,
		url:"ws://127.0.0.1:8081/simple",
		publicMsg:null,
		userMsg:null
	},
    mutations: {
		startSocket(state,token){
			let ws = new UniWebSocket(state.url)
			state.client = new Stomp.over(ws)
			state.client.connect({"token":token},data=>{
				state.client.subscribe("/topic/mymsg",(data)=>{
				state.publicMsg =  data.body
				})
				state.client.subscribe("/user/topic/mymsg",(data)=>{
				  state.userMsg = data.body
				})
			})
		},
		publicSend(state,val){
			state.client.send("/app/msgq",{},val)
		},
		sendusermsg(state,val){
		  state.client.send("/app/touser",{},val)
		},
		disconnect(state,val){
			// 断开连接
			if (state.client) {
				state.client.disconnect()
			}
		}
	},
    actions: {
		
	}
})
export default store